﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using CamHelper.Engine;
using CamHelper.Facade;
using Ninject;
using Ninject.MockingKernel.RhinoMock;
using NUnit.Framework;
using Rhino.Mocks;
using SystemWrapper.Microsoft.Win32;

namespace CamHelper.Tests.Engine
{
    [TestFixture]
    public class TopSolidWatcherTest
    {

        #region IsRunning

        [Test]
        public void IsRunning_NoProcess()
        {
            var processMock = MockRepository.GenerateMock<IProcessWrap>();
            processMock.Expect(x => x.ProcessGetProcessesByNameLength(Arg<string>.Is.Anything)).Return(0);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IProcessWrap>().ToConstant(processMock);
            var con = kernel.Get<TopSolidWatcher>();

            bool run = con.IsRunning();
            Assert.That(run, Is.False);
            processMock.VerifyAllExpectations();
        }

        [Test]
        public void IsRunning_OneTop99Process()
        {
            var processMock = MockRepository.GenerateMock<IProcessWrap>();
            processMock.Expect(x => x.ProcessGetProcessesByNameLength(Arg<String>.Is.Equal("top99"))).Repeat.Never().Return(0);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IProcessWrap>().ToConstant(processMock);
            var con = kernel.Get<TopSolidWatcher>();

            bool run = con.IsRunning();
            Assert.That(run, Is.False);
            processMock.VerifyAllExpectations();
        }

        [Test]
        public void IsRunning_OneTop69Process()
        {
            var processMock = MockRepository.GenerateMock<IProcessWrap>();
            processMock.Expect(x => x.ProcessGetProcessesByNameLength(Arg<String>.Is.Equal("top69"))).Return(1);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IProcessWrap>().ToConstant(processMock);
            var con = kernel.Get<TopSolidWatcher>();

            bool run = con.IsRunning();
            Assert.That(run, Is.True);
            processMock.VerifyAllExpectations();
        }

        [Test]
        public void IsRunning_OneTop610Process()
        {
            var processMock = MockRepository.GenerateMock<IProcessWrap>();
            processMock.Expect(x => x.ProcessGetProcessesByNameLength(Arg<String>.Is.Equal("top610"))).Return(1);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IProcessWrap>().ToConstant(processMock);
            var con = kernel.Get<TopSolidWatcher>();

            bool run = con.IsRunning();
            Assert.That(run, Is.True);
            processMock.VerifyAllExpectations();
        }

        [Test]
        public void IsRunning_OneTop611Process()
        {
            var processMock = MockRepository.GenerateMock<IProcessWrap>();
            processMock.Expect(x => x.ProcessGetProcessesByNameLength(Arg<String>.Is.Equal("top611"))).Return(1);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IProcessWrap>().ToConstant(processMock);
            var con = kernel.Get<TopSolidWatcher>();

            bool run = con.IsRunning();
            Assert.That(run, Is.True);
            processMock.VerifyAllExpectations();
        }

        [Test]
        public void IsRunning_OneTop612Process()
        {
            var processMock = MockRepository.GenerateMock<IProcessWrap>();
            processMock.Expect(x => x.ProcessGetProcessesByNameLength(Arg<String>.Is.Equal("top612"))).Return(1);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IProcessWrap>().ToConstant(processMock);
            var con = kernel.Get<TopSolidWatcher>();

            bool run = con.IsRunning();
            Assert.That(run, Is.True);
            processMock.VerifyAllExpectations();
        }

        #endregion

        [Test]
        public void GetTopSolidRegistryEnvironmentValue_NoKey()
        {
            var registryMock = MockRepository.GenerateMock<IRegistryWrap>();
            registryMock.Expect(x => x.LocalMachine.OpenSubKey(Arg<String>.Is.Anything)).Return(null);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IRegistryWrap>().ToConstant(registryMock);
            var con = kernel.Get<TopSolidWatcher>();

            var o = con.GetTopSolidRegistryEnvironmentValue(611, TopSolidRegistryEnvironmentKeys.TOPCONFIG);
            Assert.That(o, Is.EqualTo(null));
        }

        [Test]
        public void GetTopSolidRegistryEnvironmentValue_1Key()
        {
            var version = 611;
            var keyEnum = TopSolidRegistryEnvironmentKeys.TOPCONFIG;
            var keyString = keyEnum.ToString();
            var registryPath = String.Format(@"SOFTWARE\Missler Software\TopSolid by Missler Software\V{0}\Environment", version);

            var registryKeyMock = MockRepository.GenerateMock<IRegistryKeyWrap>();
            registryKeyMock.Expect(x => x.GetValue(keyString)).Return("OK");
            var registryMock = MockRepository.GenerateMock<IRegistryWrap>();
            registryMock.Expect(x => x.LocalMachine.OpenSubKey(registryPath)).Return(registryKeyMock);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IRegistryWrap>().ToConstant(registryMock);
            var con = kernel.Get<TopSolidWatcher>();

            var o = con.GetTopSolidRegistryEnvironmentValue(version, keyEnum);
            Assert.That(o, Is.EqualTo("OK"));
        }
    }
}
